import 'package:dio/dio.dart';
import 'package:flutterdome/services/network/api_service.dart';
import 'package:flutterdome/services/network/response_models.dart';

/// 示例API客户端
class ApiClient {
  final ApiService _apiService = ApiService();

  /// 获取用户信息示例
  Future<BaseResponse<Map<String, dynamic>>> getUserInfo(
    String userId,
    {
    String? baseUrl,
    Map<String, dynamic>? headers,
  }) async {
    try {
      final response = await _apiService.get<Map<String, dynamic>>(
        '/users/$userId',
        baseUrl: baseUrl,
        headers: headers,
      );
      
      return BaseResponse.fromJson(
        response.data ?? {},
        (json) => json,
      );
    } catch (e) {
      rethrow;
    }
  }

  /// 登录示例
  Future<BaseResponse<Map<String, dynamic>>> login(
    String username,
    String password,
    {
    String? baseUrl,
    Map<String, dynamic>? headers,
  }) async {
    try {
      final response = await _apiService.post<Map<String, dynamic>>(
        '/auth/login',
        baseUrl: baseUrl,
        data: {
          'username': username,
          'password': password,
        },
        headers: headers,
      );
      
      return BaseResponse.fromJson(
        response.data ?? {},
        (json) => json,
      );
    } catch (e) {
      rethrow;
    }
  }

  /// 分页获取列表示例
  Future<BaseResponse<PaginationResponse<Map<String, dynamic>>>> getItems(
    PaginationRequest request,
    {
    String? baseUrl,
    Map<String, dynamic>? headers,
  }) async {
    try {
      final response = await _apiService.get<Map<String, dynamic>>(
        '/items',
        baseUrl: baseUrl,
        queryParameters: request.toJson(),
        headers: headers,
      );
      
      return BaseResponse.fromJson(
        response.data ?? {},
        (json) => PaginationResponse.fromJson(
          json,
          (itemJson) => itemJson,
        ),
      );
    } catch (e) {
      rethrow;
    }
  }

  /// 上传文件示例
  Future<BaseResponse<Map<String, dynamic>>> uploadImage(
    UploadFileRequest request,
    {
    String? baseUrl,
    Map<String, dynamic>? headers,
    ProgressCallback? onProgress,
  }) async {
    try {
      final response = await _apiService.uploadFile<Map<String, dynamic>>(
        '/upload/image',
        request.filePath,
        fileName: request.fileName,
        fileKey: request.fileKey,
        baseUrl: baseUrl,
        data: request.extraData,
        headers: headers,
        onSendProgress: onProgress,
      );
      
      return BaseResponse.fromJson(
        response.data ?? {},
        (json) => json,
      );
    } catch (e) {
      rethrow;
    }
  }

  /// 下载文件示例
  Future<Response<dynamic>> downloadFile(
    String fileUrl,
    String savePath,
    {
    Map<String, dynamic>? queryParameters,
    Map<String, dynamic>? headers,
    ProgressCallback? onProgress,
    CancelToken? cancelToken,
  }) async {
    try {
      return await _apiService.download(
        fileUrl,
        savePath,
        queryParameters: queryParameters,
        headers: headers,
        onReceiveProgress: onProgress,
        cancelToken: cancelToken,
      );
    } catch (e) {
      rethrow;
    }
  }
}